package controls.web.implementation;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

import reporting.Report;

public class SelectInput extends Control implements
		controls.web.interfaces.SelectInput {
	Select selectInput;

	public SelectInput(String descriptiveName, By... criterion) {

		super(descriptiveName, criterion);
		selectInput = new Select(getWebElement());
	}

	@Override
	public void select(int optionId) {
		if (selectInput != null) {
			try {
				selectInput.selectByIndex(optionId);
				Report.pass("Option number " + optionId + " is selected in "
						+ this.descriptiveName);
			} catch (Exception ex) {
				Report.error("Option number " + optionId
						+ " CAN NOT be selected in " + this.descriptiveName);
			}
		} else {
			Report.error(this.descriptiveName + " is NOT initialised");
		}

	}

	@Override
	public void select(String option) {
		if (selectInput != null) {
			try {
				selectInput.selectByVisibleText(option);
				Report.pass("\"" + option + "\" is selected in "
						+ this.descriptiveName);
			} catch (Exception ex) {
				Report.error("\"" + option + "\" CAN NOT be selected in "
						+ this.descriptiveName);
			}
		} else {
			Report.error(this.descriptiveName + " is NOT initialised");
		}

	}

	@Override
	public String getSelected() {
		String selectedOption = "";
		if (selectInput != null) {
			try {
				selectedOption = selectInput.getFirstSelectedOption().getText();
				Report.info("Currently selected option in "
						+ this.descriptiveName + " is " + selectedOption);
			} catch (Exception ex) {
				Report.error("Currently selected option in "
						+ this.descriptiveName + " CAN NOT be retrived");
			}
		} else {
			Report.error(this.descriptiveName + " is NOT initialised");
		}
		return selectedOption;
	}

	@Override
	public void isSelected(String option) {
		try {
			String actualOption = selectInput.getFirstSelectedOption()
					.getText();
			if (option.equals(actualOption)) {
				Report.pass(this.descriptiveName + " selected option is "
						+ option + " as expected");
			} else {
				Report.error(this.descriptiveName + " selected option is "
						+ actualOption + " instead of " + option);
			}
		} catch (Exception ex) {
			Report.error(
					this.descriptiveName + " status CAN NOT be determined", ex);
		}

	}

	@Override
	public void isSelected(int optionId) {
		try {
			int actualId = getFirstSelectedOptionId();
			if (optionId == actualId) {
				Report.pass(this.descriptiveName + " selected option is number "
						+ optionId + " as expected");
			} else {
				Report.error(this.descriptiveName + " selected option is number "
						+ actualId + " instead of number " + optionId);
			}
		} catch (Exception ex) {
			Report.error(
					this.descriptiveName + " status CAN NOT be determined", ex);
		}

	}

	private int getFirstSelectedOptionId() {
		int firstSeletedOption = 0;
		
		List<WebElement> allOptions = selectInput.getOptions();
		WebElement firstOption = selectInput.getFirstSelectedOption();
		
		firstSeletedOption = allOptions.indexOf(firstOption);
		
		return firstSeletedOption;
	}

	@Override
	public boolean isAvailable(String option) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean areAvailable(String[] options) {
		// TODO Auto-generated method stub
		return false;
	}
}
